package com.songwo.core.web.system;

import com.songwo.core.primary.dao.system.SysUserDao;
import com.songwo.core.primary.entity.system.SysUser;
import com.songwo.exception.SwException;
import com.songwo.util.MailUtils;
import com.songwo.util.SwProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhongzheng
 */
@Controller
public class HelloController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SwProperties swProperties;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private SysUserDao sysUserDao;

    @RequestMapping("/sendMail")
    @ResponseBody
    public String mail() {
        List<SysUser> sysUsers = sysUserDao.findAll();
        Map<String, Object> map = new HashMap<>(9);
        map.put("message", "测试效果");
        map.put("imageResourceName", "测试邮件");
        map.put("name", "钟正");
        map.put("title", "标题");
        map.put("description", "描述");
        map.put("status", "状态");
        map.put("createDate", "2018-05-24");
        map.put("leftTime", "15:45:39");
        map.put("users", sysUsers);
        mailUtils.sendMail(new String[]{"410814773@qq.com"},
                null,
                "测试标题",
                "mailTemplate",
                map,
                new String[]{"D:\\daili_p01_v.jpg"});
        return "success";
    }

//    @RequestMapping("/")
//    public ModelAndView index(ModelAndView mv) {
//        logger.info("测试log");
//        mv.addObject("kaka", "kaka")
//                .addObject("randomValue", swProperties.getRandomValue())
//                .setViewName("login");
//        return mv;
//    }

    @RequestMapping("/errorTest")
    public ModelAndView error(ModelAndView mv) throws Exception {
        logger.info("测试log");
        mv.addObject("kaka", "kaka").setViewName("hello");
        throw new Exception();
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello World";
    }

    @RequestMapping("/json")
    public String json() throws SwException {
        throw new SwException("发生错误2");
    }
}
